import { defineMock } from '@alova/mock'
import { Code } from '@/utils/http/type'
import { result, resultError, resultSuccess } from './_utils'

// 定义用户信息的类型
interface UserInfo {
  id: number
  username: string
  password: string
  nickname: string
  avatar: string
  codes: string[]
  roles: string[]
}

// 定义用户对象的类型
interface Users {
  [key: string]: UserInfo
}

// 定义用户对象
const users: Users = {
  admin: {
    id: 1,
    username: 'admin',
    password: '123456',
    nickname: '超级管理员',
    avatar: '',
    codes: [],
    roles: [],
  },
}

export default defineMock({
  '[POST]/api/auth/login': ({ data }) => {
    const { username, password } = data
    const user = users[username]
    if (!user) {
      return resultError('用户不存在')
    }
    if (user.password !== password) {
      return resultError('密码错误')
    }
    return resultSuccess({
      token: username,
    })
  },
  '[POST]/api/auth/logout': () => {
    return resultSuccess()
  },
  '/api/auth/info': ({ headers }) => {
    const { Authorization } = headers
    const user = users[Authorization]
    if (!user) {
      return resultError('用户不存在')
    }
    return resultSuccess(user)
  },
  '/api/auth/timeout': () => {
    return result(Code.NOT_LOGIN, '未登录')
  },
  '/api/auth/menus': () => {
    return resultSuccess({
      menus: [
        {
          id: 1,
          path: '',
          title: '系统管理',
          icon: 'i-ep:setting',
          children: [
            {
              id: 2,
              path: '/system/admin',
              title: '用户管理',
              icon: 'i-ep:user',
            },
            {
              id: 3,
              path: '/system/role',
              title: '角色管理',
              icon: 'i-ep:coordinate',
            },
            {
              id: 4,
              path: '/system/menu',
              title: '菜单管理',
              icon: 'i-ep:menu',
            },
          ],
        },
      ],
    })
  },
})
